package com.mbds.activities;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;

import com.example.sismapp.R;
import com.mbds.accelerometer.SensorDataManager;
import com.mbds.map.NetWork;

public class SettingsActivity extends ActionBarActivity {	
	private static SharedPreferences sharedPref;
	
	private static OnPreferenceChangeListener loginChangeListener = new OnPreferenceChangeListener() {
		@Override
		public boolean onPreferenceChange(Preference preference, Object value) {
			preference.setSummary(value.toString());
			String login = sharedPref.getString(MainActivity.getLOGIN_KEY_STRING(), "");
			NetWork.getInstance().removePerson(login); //we remove the value because when the MapActivity start,
													// the fonction updateLocationUser is called
			return true;
		}
	};
	
	private static OnPreferenceChangeListener startDBCgangeListener = new OnPreferenceChangeListener() {

		@Override
		public boolean onPreferenceChange(Preference preference, Object newValue) {
			boolean value = (Boolean) newValue;
			if(value) {
				SensorDataManager.getInstance().startDataCollection();
			} else {
				SensorDataManager.getInstance().stopDataCollection();
			}
			
			return true;
		}
	};
	
	private static OnPreferenceChangeListener locationChangeListener = new OnPreferenceChangeListener() {
		
		@Override
		public boolean onPreferenceChange(Preference preference, Object newValue) {
			boolean value = (Boolean) newValue;
			if(value) {
				MapActivity.connectLocationClient();
			} else {
				MapActivity.disconnectLocationClient();
			}
			return true;
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);
		sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
		
		if (savedInstanceState == null) {
			getFragmentManager().beginTransaction()
			.add(R.id.container, new SettingFragment()).commit();
		}
				
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.settings, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

			
	/**
	 * Fragment of the settings
	 *
	 */
	public static class SettingFragment extends PreferenceFragment{
		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			
			addPreferencesFromResource(R.xml.pref_general);
			//Add a summary for the Login
			//summary = current login
			//We also add a listener to change the summary when the login change
			bindPreferenceSummaryToValue(findPreference(MainActivity.getLOGIN_KEY_STRING()));
			
			findPreference(MainActivity.getSTOP_DB_KEY()).setOnPreferenceChangeListener(startDBCgangeListener);
			findPreference(MainActivity.getLOCATION_KEY()).setOnPreferenceChangeListener(locationChangeListener);
		}
		
		private static void bindPreferenceSummaryToValue(Preference preference) {
			// Set the listener to watch for value changes.
			preference
					.setOnPreferenceChangeListener(loginChangeListener);

			// Trigger the listener immediately with the preference's
			// current value.
			loginChangeListener.onPreferenceChange(
					preference,
					PreferenceManager.getDefaultSharedPreferences(
							preference.getContext()).getString(preference.getKey(),
							""));
		}

	}

}
